package com.lc.admin;

import com.huaweicloud.sdk.iot.device.IoTDevice;
import com.huaweicloud.sdk.iot.device.client.requests.DeviceMessage;
import com.huaweicloud.sdk.iot.device.client.requests.ServiceProperty;
import com.huaweicloud.sdk.iot.device.transport.ActionListener;
import com.huaweicloud.sdk.iot.device.transport.RawMessage;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class DeviceReg {

    @Test
    public void testRegDevice() throws Exception{
        URL resource = DeviceReg.class.getClassLoader().getResource("ca.jks");
        File file = new File(resource.getPath());
        IoTDevice device = new IoTDevice(
                "ssl://9830e5aead.st1.iotda-device.cn-north-4.myhuaweicloud.com:8883",
                "68807d2fd582f200183e3bcf_1234",
                "88888888",
                file
        );

        if (device.init() != 0) {
            return;
        }
        int bat = 100;
        //定时上报属性
        while (true) {

            Map<String ,Object> json = new HashMap<>();
            //按照物模型设置属性
            json.put("电量剩余", bat--);

            ServiceProperty serviceProperty = new ServiceProperty();
            serviceProperty.setProperties(json);
            serviceProperty.setServiceId("Basic");//serviceId要和物模型一致


            device.getClient().reportProperties(Arrays.asList(serviceProperty), new ActionListener() {
                @Override
                public void onSuccess(Object context) {
                    System.out.println("上传成功");
                }

                @Override
                public void onFailure(Object context, Throwable var2) {
                    System.out.println("上传失败");
                }
            });

            Thread.sleep(10000);
        }
    }
}
